<template>
  <div>
    <div class="return" v-if="uid" @click="back">
      <img class="backImg" src="@/assets/images/back.png" alt />
    </div>
    <el-form ref="detail" :model="detail"  :rules="rules" label-position="right" label-width="130px">
      <div class="conMsg">
        <div class="title">
          <i class="el-icon-collection-tag" />基本资料
        </div>
        <div class="form">
          <el-form-item size="mini" label="销售部门:">
            <el-input v-model="detail.sales_name" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="客户姓名">
              <el-input v-model="detail.customer_name" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="护理师(开单人)">
            <el-input v-model="detail.sale_name" class="w200" readonly />
          </el-form-item>

          <el-form-item size="mini" label="业务类型" prop="business_type">
            <el-input v-model="detail.business_type" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="订单类型" prop="order_type">
            <el-input v-model="detail.order_type" class="w200" readonly />
            </el-form-item>
          <el-form-item size="mini" label="销售组织">
            <el-input v-model="detail.sales_organization" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="发货组织">
            <el-input v-model="detail.settlement_name" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="发货部门:">
            <el-input v-model="detail.settlement_bumen_name" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="发货仓库" prop="house_code">
            <el-input v-model="detail.settlement_cangku_name" class="w200" readonly />
          </el-form-item>
          <el-form-item size="mini" label="成交日期" prop="deal_time">
            <el-input v-model="detail.deal_time" class="w200" readonly />
          </el-form-item>
          
		  <el-form-item size="mini" label="收入类型" prop="income_type">
		    <el-select
		      v-model="detail.income_type"
		      placeholder="请选择收入类型"
		      class="w200"
		    >
		      <el-option
		        v-for="item in options.income_type"
		        :key="item.id"
		        :label="item.name"
		        :value="item.id"
		      />
		    </el-select>
		  </el-form-item>
		  
        </div>
      </div>
      <div class="conMsg">
        <div class="title">
          <i class="el-icon-collection-tag" />商品列表
        </div>
        <!--列表-->
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-table
              :data="goods"
              size="small"
              border
              :cell-style="$style.cellStyle"
              :header-cell-style="$style.rowClass"
              @selection-change="handleSelected"
            >
              <el-table-column type="selection" width="50" />
              <el-table-column label="* 商品名称" width="220" prop="name">
                <template slot-scope="{ row, $index }">
                  <el-select
                    v-model="row.name"
                    size="mini"
                    value-key="id"
                    placeholder="请选择商品"
                    filterable
                    :remote-method="remoteMethod"
                    @change="val => selectChange(val, $index)"
                  >
                    <el-option
                      v-for="item in product"
                      :key="item.id"
                      :label="item.zu_name"
                      :value="item.id"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="服务/商品">
                <template slot-scope="{ row }">
                  <div v-if="row.is_project == 1">商品</div>
                  <div v-if="row.is_project == 2">服务</div>
                </template>
              </el-table-column>
              <el-table-column label="规格" prop="spec_name" />
              <el-table-column label="* 是否赠品">
                <template >
                    <div >是</div>
                </template>
              </el-table-column>
              <el-table-column label="标准价格" prop="goods_price" />

              <el-table-column label="* 销售价格" width="160">
                <template scope="{ row}">
                    <div >{{row.pay_price}}</div>
                </template>
              </el-table-column>
              <el-table-column label="* 数量" width="160">
                <template scope="{ row, $index }">
                  <el-input-number
                    v-if="row.is_project == 1 && row.kingdee_code != '101.12.099'&& row.kingdee_code != '101.12.098'"
                    v-model="row.number"
                    :precision="2"
                    size="mini"
                    :min="0.01"
                    @change="(value) => handleChange(value, $index, 'goods')"
                  />
                  <el-input-number
                    v-if="(row.is_project == 2 || row.kingdee_code == '101.12.099') && row.kingdee_code != '101.12.098' "
                    v-model="row.number"
                    size="mini"
                    :min="1"
                    @change="(value) => handleChange(value, $index, 'goods')"
                  />
                  <el-input-number
                    v-if="row.kingdee_code == '101.12.098'"
                    v-model="row.number"
                    size="mini"
                    @change="(value) => handleChange(value, $index, 'goods')"
                  />
                </template>
              </el-table-column>
              <el-table-column label="销售金额" >
                  <template >
                    <div >0.00</div>
                </template>
              </el-table-column>
              <el-table-column label="* 商品/服务类型" width="150">
                <template slot-scope="{ row }">
                  <el-radio
                    v-if="row.is_project == 2 &&( row.kingdee_code != '101.12.098' &&  row.kingdee_code != '101.12.099')"
                    v-model="row.project_type"
                    :label="1"
                  >按次</el-radio>
                  <el-radio
                    v-if="row.is_project == 2 && (row.kingdee_code != '101.12.098' && row.kingdee_code != '101.12.099')"
                    v-model="row.project_type"
                    :label="2"
                  >按天</el-radio>
                  <span v-if="row.is_project == 1  && (row.kingdee_code != '101.12.098' && row.kingdee_code != '101.12.099')">--</span>
                </template>
              </el-table-column>
              <el-table-column label="* 服务次/天数" width="250">
                <template scope="{ row, $index }">
                  <el-input
                    v-if="
                      row.project_type == 1 &&
                      row.is_project == 2
                    "
                    v-model="row.service_number"
                    class="w150"
                    size="mini"
                    type="span"
                  >
                    <span slot="append">次</span>
                  </el-input>
                  <el-date-picker
                    v-model="row.serveData"
                    type="daterange"
                    v-if="
                      row.project_type == 2 &&
                      row.is_project == 2
                    "
                    size="mini"
                    class="w220"
                    @change="(value) => timeChange(value, $index, 'goods')"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                  ></el-date-picker>
                  <span v-if="row.is_project == 1">--</span>
                </template>
              </el-table-column>
            </el-table>
            <div class="flexSb mt20">
              <div>
                <el-button
                  type="success"
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleProduct('add')"
                >新增</el-button>
                <el-button
                  type="danger"
                  icon="el-icon-minus"
                  size="mini"
                  @click="handleProduct('reduce')"
                >移除</el-button>
              </div>
              <div class="mr20">
                合计:
                <!-- <span class="peril">{{ detail.orderTotal }}元</span> -->
                <span class="peril">0元</span>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>

      <div class="submit">
        <el-button type="primary" size="mini" class="mainBtn" @click="submitForm('detail')">提交订单</el-button>
      </div>
    </el-form>
    <el-dialog
      title="结算统计"
      :visible.sync="privilegePopup"
      :close-on-click-modal="false"
      width="1000px"
      @close="closeTicket"
    >
      <div v-if="leadData">
        <div class="mb10 fwb" style="display: flex;justify-content: space-between;">
          <span>请选择优惠券:</span>
          <el-button size="mini" type="primary" @click="totalMoney('clearGoods')">不使用优惠券</el-button>
        </div>
        <div class="privilegeList">
          <div
            v-for="item in leadData"
            :key="item.id"
            @click="tapPrivilege(item)"
            :class="item.choice?'':'choice' "
            class="privilegeItem"
          >
            <div class="privilegeL">
              <div class="privilegeMoney">￥{{item.denomination}}</div>
              <div class="round1"></div>
              <div class="round2"></div>
            </div>
            <div class="privilegeR">
              <div class="privilegeName">
                <span>{{item.name}}</span>
                <span v-if="item.type==1" class="fz12">类型：满减券</span>
                <span v-if="item.type==3" class="fz12">类型：满赠券</span>
              </div>
              <div class="privilegeFull">
                使用门槛：
                <span v-if="item.use_threshold>0">满{{item.use_threshold}}可用</span>
                <span v-else>无门槛</span>
              </div>
              <div class="privilegeTime">
                有效期：
                <span v-if="item.end_time">{{item.start_time}}至{{item.end_time}}</span>
                <span v-else>无限制</span>
              </div>
            </div>
          </div>
        </div>
        <el-row type="flex" justify="space-between" class="mt20">
          <el-col :span="24">
            <el-table
              :data="privilegeList"
              size="small"
              border
              :cell-style="$style.cellStyle"
              :header-cell-style="$style.rowClass"
            >
              <ElTableColumn label="商品名称" prop="name" fixed width="160" />
              <el-table-column label="服务/商品">
                <template slot-scope="{ row }">
                  <div v-if="row.is_project == 1">商品</div>
                  <div v-if="row.is_project == 2">服务</div>
                </template>
              </el-table-column>
              <el-table-column label="规格" prop="spec_name" />
              <el-table-column label="标准价格" prop="goods_price" />
              <el-table-column label="* 销售价格" width="100">
                <template scope="{$index}">
                  <span v-if="privilegeType == 1">{{privilegeList[$index].goods_price}}</span>
                  <span v-if="privilegeType == 3">0</span>
                </template>
              </el-table-column>
              <el-table-column label="* 数量" width="160">
                <template scope="{$index}">
                  <el-input-number
                    v-if="privilegeType == 3"
                    v-model="privilegeList[$index].number"
                    :precision="2"
                    size="mini"
                    @change="(value) => handleChange(value, $index,'privilege')"
                  />
                  <span v-if="privilegeType == 1">1</span>
                </template>
              </el-table-column>
              <el-table-column label="销售金额" prop="littleCount" />
              <el-table-column label="* 是否赠品" size="mini" width="100">
                <template>
                  <span>赠品</span>
                </template>
              </el-table-column>
              <el-table-column label="* 商品/服务类型" width="150">
                <template slot-scope="{ row }">
                  <el-radio
                    v-if="row.is_project == 2 && (row.kingdee_code != '101.12.098' && row.kingdee_code != '101.12.099')"
                    v-model="row.project_type"
                    label="1"
                  >按次</el-radio>
                  <el-radio
                    v-if="row.is_project == 2 && (row.kingdee_code != '101.12.098' && row.kingdee_code != '101.12.099')"
                    v-model="row.project_type"
                    label="2"
                  >按天</el-radio>
                  <span v-if="row.is_project == 1 && (row.kingdee_code != '101.12.098' && row.kingdee_code != '101.12.099')">--</span>
                </template>
              </el-table-column>
              <el-table-column label="* 服务次/天数" width="300">
                <template slot-scope="{ row }">
                  <el-input
                    v-if="
                      row.project_type == 1 &&
                      row.is_project == 2
                    "
                    v-model="row.service_number"
                    class="w150"
                    size="mini"
                    type="span"
                  >
                    <span slot="append">次</span>
                  </el-input>
                  <el-date-picker
                    v-model="row.start_time"
                    type="date"
                    v-if="
                      row.project_type == 2 &&
                      row.is_project == 2
                    "
                    size="mini"
                    class="w130"
                    placeholder="开始日期"
                    value-format="yyyy-MM-dd"
                  ></el-date-picker>

                  <el-date-picker
                    v-model="row.end_time"
                    type="date"
                    v-if="
                      row.project_type == 2 &&
                      row.is_project == 2
                    "
                    size="mini"
                    class="w130"
                    placeholder="结算日期"
                    value-format="yyyy-MM-dd"
                  ></el-date-picker>
                  <span v-if="row.is_project == 1">--</span>
                </template>
              </el-table-column>
            </el-table>
            <div class="flexSb mt20">
              <div></div>
              <div class="mr20 mb20">
                优惠合计:
                <span class="peril">{{ privilegeTotal }}元</span>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      <div style="text-align: right;" class="fwb">
        （
        <span>实收本金：￥{{detail.business_type == 1?accounts.zc_goods_y:accounts.zc_fuwu_y}}</span>
        +
        <span>实收赠金：￥{{detail.business_type == 1?accounts.zc_goods_z:accounts.zc_fuwu_z}}</span>
        )
        <span class="ml20">
          实收总额：
          <span
            class="peril"
          >￥{{detail.business_type == 1?accounts.zc_allgoods_z:accounts.zc_allfuwu_z}}</span>
        </span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="isPrivilege">确定提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { goodsList } from '@/api/goods'
import { getCardDetail } from '@/api/card'
import { staffListS } from '@/api/staff'
import { shopMarket, shopStore } from '@/api/shop'
import {
  getCouponProvideCustomer,
  createOrder,
  createYhjOrder,
  getPayment,
  getCouponProvide,
  saleList
} from '@/api/order'
class Detail {
  sales_name = ''; // 销售部门
  uid = ''; // 客户id
  customer_name = ''; // 客户名称
  sale_name = ''; // 护理师
  business_type = ''; // 业务类型
  order_type = ''; // 订单类型
  income_type = ''; // 收入类型
  sales_organization = ''; // 销售组织
  settlement_name = ''; // 发货组织编码       
  settlement_bumen_name = ''; // 发货部门       
  settlement_cangku_name = ''; // 发货仓库       
  deal_time = ''; // 订单成交日
  goodsList = [];
}

class Options {
	income_type = [
	  { id: 1, name: '服务（各类疗程卡项及疗程卡项赠送产品）' },
	  { id: 2, name: '产品销售（单独付费购买产品）' },
	  { id: 3, name: '医美收入' }
	];
	
}
class Rules {
	income_type = [
	  { required: true, message: '请选择收入类型', trigger: 'change' }
	]
}
class Goods {
  ids = Math.round(Math.random() * 1234567890);
  id = '';
  name = '';
  is_coupon_goods = 0; // 是否优惠券商品0否 1是
  is_project = '';
  kingdee_code = '';
  spec_name = '';
  goods_price = '';
  pay_price = 0;
  number = 0;
  project_type = 1;
  is_gift = 0;
  checked = true;
  house_code = '';
  service_number = '';
  start_time = '';
  end_time = '';
  unit_code = '';
  serveData = []; // 服务天数
}
export default {
  data () {
    return {
      detail: new Detail(), // 信息
	  options: new Options(), // 选项
	  rules: new Rules(), // 表单校验
      leadData: [],
      data: [{}],
      goods: [new Goods()], // 商品列表
      uid : '', // 客户id
      product: [], // 商品列表
      select: [], // 选择id
      privilegeData: '', // 优惠券服务天数
      loading: false, // resource
      privilegePopup: false, // 优惠券弹窗
      privilegeList: [], // 优惠券详情商品列表
      privilegeType: '', // 优惠券详情类型
      privilegeTotal: 0, // 优惠券合计
      accounts: { yhj_list: [] } // 结算清单
    }
  },
  mounted () {
    // this.detail.shop_id = this.$route.query.detail.shop_id
    // this.detail.shop_name = this.$route.query.detail.shop_name
    this.uid = this.$route.query.detail.id
    this.getList()
  },
  methods: {
    // 获取详情
    getList () {
        getCouponProvideCustomer({yhj_id:this.$route.query.yhj_id}).then(res => {
        // console.log(res.data)
        this.goods = res.data.goodsList
        // console.log(this.goods)
        this.detail = res.data
      })
       goodsList({ page: 1, limit: 100000, zu_name:  '' }).then(
        res => {
          this.product = res.list
        }
      )
    },
    // 更改删除项
    handleSelected (row) {
      this.select = row.map(val => val.id)
    },
    // 商品操作
    handleProduct (type) {
      switch (type) {
        case 'add':
          let tip = 0
          this.goods.forEach(item => {
            if (item.id == '') {
              tip += 1
            }
          })
          if (tip > 0) {
            this.$message({
              message: '请补全列表中为空商品。',
              type: 'warning'
            })
          } else {
            this.goods.push(new Goods())
          }
          break
        case 'reduce':
          if (this.select.length === 0) {
            this.$message({
              message: '请选择想要移除的商品项。',
              type: 'warning'
            })
          }
          this.goods = this.goods.filter(
            val => this.select.findIndex(item => item === val.id) < 0
          )
          this.getSummaries('goods')
          break
      }
    },
    // 销售结果更改
    priceChange (value, index, type) {
      if (type == 'goods') {
        this.goods[index].littleCount = (
          value * this.goods[index].number
        ).toFixed(2)
        this.getSummaries(type)
      } else {
        this.privilegeList[index].littleCount = (
          value * this.privilegeList[index].number
        ).toFixed(2)
        this.getSummaries(type)
      }
    },
    // 商品数量
    handleChange (value, index, type) {
      if (type == 'goods') {
        this.goods[index].littleCount = (
          value * this.goods[index].pay_price
        ).toFixed(2)
        this.getSummaries('goods')
      } else {
        this.privilegeList[index].littleCount = (
          value * this.privilegeList[index].pay_price
        ).toFixed(2)
        this.getSummaries('privilege')
      }
    },
    // 搜索商品
    remoteMethod (e) {
      this.getGoods(e)
    },
    // 选择商品
    selectChange (value, index) {
      const cur = this.product.find(item => item.id === value)
      this.$set(this.goods, index, Object.assign({ is_gift: 0 }, this.goods[index], cur))
      this.goods[index].number = 1.00
      this.handleChange (this.goods[index].number, index, 'goods')
      
    },
    // 是否赠品
    changeGift (value, index) {
      this.goods[index].pay_price = 0
      this.goods[index].number = 1
      this.goods[index].is_gift = this.goods[index].checked ? 1 : 0
      this.priceChange(this.goods[index].pay_price, index, 'goods')
    },
    // 合计计算
    getSummaries (type, againCount) {
      if (type == 'goods') {
        this.detail.orderTotal = 0
        this.goods.forEach(item => {
          this.detail.orderTotal = (
            parseFloat(this.detail.orderTotal ? this.detail.orderTotal : 0) +
            parseFloat(item.littleCount ? item.littleCount : 0)
          ).toFixed(2)
        })
      } else {
        this.privilegeTotal = 0
        this.privilegeList.forEach(item => {
          this.privilegeTotal = (
            parseFloat(this.privilegeTotal ? this.privilegeTotal : 0) +
            parseFloat(item.littleCount ? item.littleCount : 0)
          ).toFixed(2)
        })
        this.privilegeTotal = Math.abs(this.privilegeTotal)
        if (againCount == 'againCount') {
          this.totalMoney('againCount')
        }
      }
    },
    // 改变开始结束时间
    timeChange (value, index, type) {
      if (type == 'goods') {
        this.goods[index].start_time = this.goods[index].serveData[0]
        this.goods[index].end_time = this.goods[index].serveData[1]
      } else {
        this.privilegeList[index].start_time = this.privilegeList[
          index
        ].privilegeData[0]
        this.privilegeList[index].end_time = this.privilegeList[
          index
        ].privilegeData[1]
      }
    },

    // 确定新增
    submitForm (formName) {
      this.detail.goodsList = this.goods.filter(val => val.id)
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.checking()
        } else {
          return false
        }
      })
    },
    // 提交校验
    checking () {
      if (this.detail.goodsList == 0) {
        this.$message({
          message: '请添加商品',
          type: 'error'
        })
      } else {
        let number = 0
        let pay_price = 0
        let project_type = 0
        let is_gift = 0
        let service_number = 0
        let start_time = 0
        this.detail.goodsList.forEach(item => {
          if (!item.number) {
            number += 1
          }
          if (item.pay_price === '' && item.is_gift != 1) {
            pay_price += 1
          }
          if (item.is_gift == 2) {
            is_gift += 1
          }

          if (item.is_project == 2 && item.id != 170 && item.id != 171) {
            if (!item.project_type) {
              project_type += 1
            }
            if (
              item.project_type == 1 &&
              !item.service_number &&
              item.id != 170 && item.id != 171
            ) {
              service_number += 1
            }
            if (item.project_type == 2 && !item.start_time) {
              start_time += 1
            }
          }
        })
        if (
          pay_price > 0 ||
          is_gift > 0 ||
          project_type > 0 ||
          service_number > 0 ||
          start_time > 0
        ) {
          this.$message({
            message: '请完善商品列表',
            type: 'error'
          })
        } else if (number > 0) {
          this.$message({
            message: '商品数量不可为0',
            type: 'error'
          })
        } else {
        //   this.totalMoney()
          this.hint('使用此兑换券')
        }
      }
    },
    // 提价兑换券
    async submitExchange(){
		//return;
        const loading = this.$loading({
        lock: true,
        text: '提交中，请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      try {
        const res = await createYhjOrder({ yhj_id: this.$route.query.yhj_id,
        ...this.detail })
        loading.close()
        console.log(res)
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.back()
      } catch (e) {
        loading.close()
      }
        
    },
    // 请求合计
    totalMoney (againCount) {
      const sendData = {}
      getPayment({
        order_amout: this.detail.orderTotal,
        customer_id: this.detail.uid,
        yhj_amount: this.privilegeTotal,
        business_type: this.detail.business_type
      }).then(res => {
        if (againCount == 'againCount') {
          this.accounts = res.data
        } else if (againCount == 'clearGoods') {
          this.accounts = res.data
          const arr = res.data.yhj_list
          arr.forEach(item => {
            item.choice = true
          })
          this.leadData = arr
          this.privilegeList = []
          this.getSummaries('privilege', 'againCount')
        } else {
          this.accounts = res.data
          const arr = res.data.yhj_list
          arr.forEach(item => {
            item.choice = true
          })
          this.leadData = arr
          this.privilegePopup = true
        }
      })
    },
    getFavorable () {
      this.privilegePopup = true
      getCouponProvide({ customer_id: this.detail.uid }).then(res => {
        let leadData = []
        if (this.detail.business_type == 1) {
          leadData = res.data.filter(val => val.use_type == 1)
        } else {
          leadData = res.data.filter(val => val.use_type == 2)
        }
        leadData.forEach(item => {
          item.choice = true
        })
        this.leadData = leadData
      })
    },
    // 关闭弹窗
    closeTicket (e) {
      this.privilegeTotal = 0
      this.leadData = []
      this.privilegeList = []
    },
    // 点击优惠券
    tapPrivilege (val) {
      this.leadData.forEach(item => {
        if (item.coupon_id != val.coupon_id) {
          item.choice = false
        } else {
          item.choice = true
          this.detail.coupon_id = item.coupon_id
          this.detail.coupon_name = item.name
        }
      })
      this.privilegeList = []
      this.privilegeTotal = 0
      this.getPrivilegeDetail(val)
    },
    // 优惠券详情
    getPrivilegeDetail (val) {
      getCardDetail({ id: val.coupon_id, type: 1 }).then(res => {
        this.privilegeType = res.type
        const list = res.goodsList
        list.forEach(item => {
          item.number = 1
          item.privilegeData = ''
          item.start_time = ''
          item.end_time = ''
          item.is_coupon_goods = 1
          item.is_gift = '1'
          item.desc = '创建订单'
          item.coupon_type = this.privilegeType
          item.house_code = this.detail.house_code
          item.house_code_name = this.detail.house_code_name
          if (this.privilegeType == 3) {
            item.pay_price = 0
            item.littleCount = 0
          } else {
            item.pay_price = item.goods_price
            item.littleCount = item.goods_price * 1
          }
        })
        this.privilegeList = list
        this.getSummaries('privilege', 'againCount')
      })
      this.detail.yhj_id = val.id
    },

    // 点击提交订单
    isPrivilege () {
      if (this.privilegeList.length && this.privilegeType == 1) {
        this.submitPrivilege()
      } else {
        this.submitOrder()
      }
    },
    // 提交优惠券
    submitPrivilege () {
      let project_type = 0
      let service_number = 0
      let start_time = 0
      let end_time = 0
      this.privilegeList.forEach(item => {
        if (item.is_project == 2) {
          if (
            !item.project_type &&
            !item.service_number &&
            item.id != 170 && item.id != 171
          ) {
            project_type += 1
          }
          if (
            item.project_type == 1 &&
            !item.service_number &&
            !item.service_number &&
            item.id != 170 && item.id != 171
          ) {
            service_number += 1
          }
          if (item.project_type == 2 && !item.start_time) {
            start_time += 1
          }
          if (item.project_type == 2 && !item.end_time) {
            end_time += 1
          }
        }
      })
      if (
        project_type > 0 ||
        service_number > 0 ||
        start_time > 0 ||
        end_time > 0
      ) {
        this.$message({
          message: '请完善商品列表',
          type: 'error'
        })
      } else {
        this.submitOrder()
      }
    },
    // 提交
    submitOrder () {
      this.detail.goodsList.forEach(item => {
        item.house_code = this.detail.house_code
        item.house_code_name = this.detail.house_code_name
        item.is_coupon_goods = 0
        item.desc = '创建订单'
      })
      this.detail.goodsList = this.detail.goodsList.concat(this.privilegeList)
      this.detail.total_amount = this.detail.orderTotal
      this.detail.discount_amount = this.privilegeTotal
      this.detail.order_amount =
        this.detail.business_type == 1
          ? this.accounts.zc_allgoods_z
          : this.accounts.zc_allfuwu_z
      this.detail.pay_amount =
        this.detail.business_type == 1
          ? this.accounts.zc_goods_y
          : this.accounts.zc_fuwu_y
      this.detail.gift_amount =
        this.detail.business_type == 1
          ? this.accounts.zc_goods_z
          : this.accounts.zc_fuwu_z
      this.hint('创建此订单')
    },
    // 提示
    hint (text) {
      this.$confirm(`${text}，是否确定？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
        //   this.confirm()
          this.submitExchange()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 确定新增
    async confirm () {
      const loading = this.$loading({
        lock: true,
        text: '提交中，请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      try {
        const res = await createOrder({ ...this.detail })
        loading.close()
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.back()
      } catch (e) {
        loading.close()
      }
    },

    // 返回
    back () {
      const query = {
        detail: this.$route.query.detail,
        id: this.$route.query.detail.id,
        activeName: 'sixth'
      }
      this.$router.push({ name: '客户详情', query })
    }
  }
}
</script>

<style lang="scss" scoped>
.privilegeList {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .privilegeItem {
    width: 460px;
    height: 110px;
    color: #33bea7;
    border-radius: 5px;
    background: #e5f7f4;
    display: flex;
    margin-bottom: 20px;
    cursor: pointer;
    .privilegeL {
      border-right: 1px dotted #33bea7;
      width: 100px;
      height: 100%;
      position: relative;
      padding: 40px 10px;
      text-align: center;
      box-sizing: border-box;
      .privilegeMoney {
        font-size: 20px;
        color: darkcyan;
      }
      .round1 {
        background: #fff;
        position: absolute;
        top: -10px;
        right: -10px;
        width: 20px;
        height: 20px;
        border-radius: 50%;
      }
      .round2 {
        background: #fff;
        position: absolute;
        bottom: -10px;
        right: -10px;
        width: 20px;
        height: 20px;
        border-radius: 50%;
      }
    }
    .privilegeR {
      padding: 20px 24px;
      width: 360px;
      box-sizing: border-box;
      .privilegeName {
        font-size: 15px;
        font-weight: bold;
        color: darkcyan;
        display: flex;
        justify-content: space-between;
      }
      .privilegeFull {
        margin-top: 20px;
        text-align: right;
      }
      .privilegeTime {
        margin-top: 10px;
        font-size: 12px;
        text-align: right;
      }
    }
  }
  .privilegeItem:hover {
    box-shadow: 0px 0px 10px #ddd;
  }
  .choice {
    background: #eee;
    div {
      color: #999 !important;
    }
    .privilegeL {
      border-right: 1px dotted #999;
    }
  }
}
.return {
  position: fixed;
  right: 20px;
  top: 71px;
  z-index: 999;
  .backImg {
    margin-right: 14px;
    width: 32px;
    height: 100%;
    cursor: pointer;
    vertical-align: middle;
  }
}

::v-deep .el-form-item--mini .el-form-item__content,
.el-form-item--mini .el-form-item__label {
  margin-left: 10px !important;
}
.conMsg {
  margin-bottom: 30px;

  .title {
    font-weight: bold;
    font-size: 14px;
    line-height: 34px;
    color: #666;
  }

  .form {
    display: flex;
    flex-wrap: wrap;

    ::v-deep .el-form-item {
      margin-right: 20px;
      display: flex;
    }
  }
  ::v-deep .el-form-item--mini.el-form-item,
  .el-form-item--small.el-form-item {
    width: 30%;
  }
}
.submit {
  padding-left: 38%;
  .mainBtn {
    margin: 50px 20px;
  }
}
.el-radio {
  margin-right: 8px;
}
.flexSb {
  display: flex;
  justify-content: space-between;
  .mr20 {
    font-weight: bold;
  }
}
</style>
